﻿Imports System.Windows.Forms
Imports System.Collections.ObjectModel

Public Class Plugins
    Private M_Set As UCBase.ResourceManager
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "Plugins *.Ucp|*.Ucp|Dll's *.Dll|*.Dll"
        If M_Set.PluginPath > "" Then OpenFileDialog1.InitialDirectory = M_Set.PluginPath

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim F As String = OpenFileDialog1.FileName
            Dim File As String = IO.Path.GetFileName(F)
            Dim Path As String = IO.Path.GetDirectoryName(F)
            Dim Desc As String = ""
            Dim Version As String = ""
            Dim Devs As String = ""

            Dim Plug As UCBase.I_Plugin
            Plug = LoadPlugIn(F)
            If Plug IsNot Nothing Then

                Dim Info As FileVersionInfo
                Info = FileVersionInfo.GetVersionInfo(F)
                Version = Info.ProductVersion
                Desc = Info.Comments

                Devs = Plug.Devices.Count.ToString
                Plug.Unload()
                Plug = Nothing
            End If
            Dim LVI As New ListViewItem(File)
            LVI.SubItems.Add(Desc)
            LVI.SubItems.Add(Version)
            LVI.SubItems.Add(Devs)
            LVI.SubItems.Add(Path)
            ListView1.Items.Add(LVI)
        End If
        SaveSet()
    End Sub
    Private Function LoadPlugIn(ByVal LoadPath As String) As UCBase.I_Plugin
        Dim NewPlugIn As UCBase.I_Plugin
        Try
            Dim PlugInAssembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(LoadPath)
            Dim Types() As Type
            Dim FoundInterface As Type
            Types = PlugInAssembly.GetTypes
            For Each PlugInType As Type In Types
                FoundInterface = PlugInType.GetInterface("UCFBase.I_Plugin")
                If FoundInterface IsNot Nothing Then
                    NewPlugIn = DirectCast(PlugInAssembly.CreateInstance(PlugInType.FullName), UCBase.I_Plugin)
                    Return NewPlugIn
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function

    Private Sub Plugins_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CheckBox1.Checked = M_Set.ScanPluginFolder
        If M_Set.ScanPluginFolder Then
            ScanPluginFolder()
        Else
            LoadSet()
        End If
    End Sub
    Private Sub LoadSet()
        If M_Set.PluginSet = "" Then Exit Sub
        Label2.Text = M_Set.PluginSet
        Dim FS As New IO.FileStream(M_Set.PluginSet, IO.FileMode.Open)
        Dim Reader As New IO.StreamReader(FS)
        ListView1.Items.Clear()
        Do While Reader.EndOfStream = False
            Dim F As String = Reader.ReadLine
            Dim File As String = IO.Path.GetFileName(F)
            Dim Path As String = IO.Path.GetDirectoryName(F)
            Dim Desc As String = ""
            Dim Version As String = ""
            Dim Devs As String = ""

            Dim Plug As UCBase.I_Plugin
            Plug = LoadPlugIn(F)
            If Plug IsNot Nothing Then
                Dim Info As FileVersionInfo
                Info = FileVersionInfo.GetVersionInfo(F)
                Version = Info.ProductVersion
                Desc = Info.Comments

                Devs = Plug.Devices.Count.ToString
                Plug.Unload()
                Plug = Nothing
            End If
            Dim LVI As New ListViewItem(File)
            LVI.SubItems.Add(Desc)
            LVI.SubItems.Add(Version)
            LVI.SubItems.Add(Devs)
            LVI.SubItems.Add(Path)
            ListView1.Items.Add(LVI)
        Loop
        Reader.Close()
    End Sub
    Private Sub ScanPluginFolder()

        Dim files As ReadOnlyCollection(Of String)
        files = My.Computer.FileSystem.GetFiles(M_Set.PluginPath, FileIO.SearchOption.SearchAllSubDirectories, New String() {"*.ucp", "*.dll"})

        For Each F As String In files
            Dim File As String = IO.Path.GetFileName(F)
            Dim Path As String = IO.Path.GetDirectoryName(F)
            Dim Desc As String = ""
            Dim Version As String = ""
            Dim Devs As String = ""
            Dim Plug As UCBase.I_Plugin
            Plug = LoadPlugIn(F)
            If Plug IsNot Nothing Then
                Dim Info As FileVersionInfo
                Info = FileVersionInfo.GetVersionInfo(F)
                Version = Info.ProductVersion
                Desc = Info.Comments

                Devs = Plug.Devices.Count.ToString
                Plug.Unload()
                Plug = Nothing
            End If
            Dim LVI As New ListViewItem(File)
            LVI.SubItems.Add(Desc)
            LVI.SubItems.Add(Version)
            LVI.SubItems.Add(Devs)
            LVI.SubItems.Add(Path)
            ListView1.Items.Add(LVI)
        Next
    End Sub
    Private Sub SaveSet()
        If Label2.Text = "" Then Exit Sub
        Dim FS As New System.IO.FileStream(Label2.Text, IO.FileMode.Create)
        Dim Writer As New IO.StreamWriter(FS)
        For Each I As ListViewItem In ListView1.Items
            Dim F As String
            Dim File As String = I.Text
            Dim Path As String = I.SubItems(4).Text
            F = IO.Path.Combine(Path, File)
            Writer.WriteLine(F)
        Next
        Writer.Close()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.Filter = "Plugin Set File *.Psf|*.Psf"
        If M_Set.PluginSet = "" Then
            If M_Set.SettingsPath > "" Then OpenFileDialog1.InitialDirectory = M_Set.SettingsPath
        Else
            OpenFileDialog1.InitialDirectory = IO.Path.GetDirectoryName(M_Set.PluginSet)
        End If
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            M_Set.PluginSet = OpenFileDialog1.FileName
            LoadSet()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SaveFileDialog1.Filter = "Plugin Set File *.Psf|*.Psf"
        If M_Set.PluginSet = "" Then
            If M_Set.SettingsPath > "" Then SaveFileDialog1.InitialDirectory = M_Set.SettingsPath
        Else
            SaveFileDialog1.FileName = M_Set.PluginSet
        End If
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            M_Set.PluginSet = SaveFileDialog1.FileName
            Label2.Text = SaveFileDialog1.FileName
            SaveSet()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim I As Integer = ListView1.SelectedIndices(0)
        ListView1.Items.RemoveAt(I)
        SaveSet()
    End Sub
    Public Property PluginSet() As UCBase.ResourceManager
        Get
            Return M_Set
        End Get
        Set(ByVal value As UCBase.ResourceManager)
            M_Set = value
        End Set
    End Property

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        M_Set.ScanPluginFolder = CheckBox1.Checked
    End Sub
End Class
